﻿using System.Net.Sockets;
using System.Net;
using System.Text;
using Vampirewal.Core.SocketCore;

namespace Test.SocketClient
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, client!");

            // 创建一个Socket并连接到服务器
            IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, 12345);
            Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            

            try
            {
                sender.Connect(remoteEP);
                Console.WriteLine("请输入要发送的消息!");
                while (true)
                {
                    var input= Console.ReadLine();

                    if (!string.IsNullOrEmpty(input))
                    {
                        if (input == "exit")
                        {
                            break;
                        }
                        else
                        {
                            for (int i = 0; i < 10; i++)
                            {

                                // 发送数据
                                string message = $"{input}-{i}";
                                byte[] msg = Encoding.ASCII.GetBytes(message);
                                int bytesSent = sender.Send(msg);
                                Console.WriteLine($"Sent: {message}");

                                //// 接受数据
                                //byte[] buffer = new byte[1024];
                                //int bytesRec = sender.Receive(buffer);
                                //string data = Encoding.ASCII.GetString(buffer, 0, bytesRec);
                                //Console.WriteLine($"Received: {data}");

                            }

                            Console.WriteLine("请输入要发送的消息!");
                        }

                    }

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                // 关闭连接
                if (sender.Connected)
                {
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();
                    Console.WriteLine("Connection closed");
                }
            }


            Console.ReadKey();
        }
    }
}
